//
//  Insert.m
//  HBHook
//
//  Created by hibo on 2019/10/19.
//  Copyright © 2019 hibo. All rights reserved.
//

#import "Insert.h"
#import <objc/message.h>
//#import <UIKit/UIKit.h>

@implementation Insert


+(void)common{
    //    Method old_method = class_getInstanceMethod(objc_getClass("WCAccountLoginControlLogic"), sel_registerName("onFirstViewLogin"));
    //    Method new_method = class_getInstanceMethod(self, sel_registerName("my_onFirstViewLogin"));
    //    method_exchangeImplementations(old_method, new_method);
    
    //1、向原始类添加方法并交换-IMP设置到该类中
    //    Method old_method = class_getInstanceMethod(objc_getClass("WCAccountLoginControlLogic"), sel_registerName("onFirstViewLogin"));
    //    BOOL result = class_addMethod(objc_getClass("WCAccountLoginControlLogic"),
    //                    sel_registerName("new_method"), (IMP)new_method, "v@:");//添加新的方法
    //    NSLog(@"%@",result?@"添加成功":@"添加失败");
    //    method_exchangeImplementations(old_method, class_getInstanceMethod(objc_getClass("WCAccountLoginControlLogic"),sel_registerName("new_method")));
    
    //    //2、方法设置
    //    old_imp = class_getMethodImplementation(objc_getClass("WCAccountLoginControlLogic"), sel_registerName("onFirstViewLogin"));
    ////    method_setImplementation(class_getInstanceMethod(objc_getClass("WCAccountLoginControlLogic"), sel_registerName("onFirstViewLogin")), (IMP)new_method);
    //        //3、替换
    //    class_replaceMethod(objc_getClass("WCAccountLoginControlLogic"), sel_registerName("onFirstViewLogin"), (IMP)new_method, "v@:");
    
}

-(void)my_onFirstViewLogin {
    NSLog(@"我来了");
    //    [self my_onFirstViewLogin];
}

IMP (*old_imp)(id self,SEL _cmd);

void new_method(id self, SEL _cmd){
    NSLog(@"我来了");
    //1、调用原始方法
    //[self performSelector:sel_registerName("new_method")];
    //2、执行函数
    old_imp(self,_cmd);
}

+ (void)load {
    NSLog(@"插入成功");
    [self modifyWxStep];
}
+(void)modifyWxStep{
    //修改微信步数
    Class class = objc_getClass("WCDeviceStepObject");
    SEL select = sel_registerName("m7StepCount");
    
    Method method = class_getInstanceMethod(class, select);
    const char *typeEncoding = method_getTypeEncoding(method);
    NSLog(@"typeEncoding:%s",typeEncoding);
    class_replaceMethod(class, select, (IMP)my_m7StepCount, typeEncoding);
}
int my_m7StepCount(id self, SEL _cmd){
    return 56382;
}

@end
